<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <link rel="stylesheet" href="style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">商品目录</div>
                <div class="layui-card-body">
                    <div style="margin-bottom: 10px;">
                        <div class="layui-input-item" style="float:left;padding-right:10px;">
                            <form class="layui-form">
                                <div class="layui-inline">
                                <input id="search-common-name" type="text"
                                       placeholder="请输入通用名" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                <input id="search-product-name" type="text"
                                       placeholder="请输入商品名" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                <input id="search-spec" type="text"
                                       placeholder="请输入规格" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                <select id="search-state">
                                    <option value="">请选择状态</option>
                                    <option value="1">启用</option>
                                    <option value="2">禁用</option>
                                </select>
                                </div>
                                <div class="layui-inline">
                                    <a id="btn-search" class="layui-btn">查找</a>
                                </div>
                            </form>
                        </div>
                        <div style="float:right;">
                            <a id="btn-add" class="layui-btn">添加</a>
                            <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <table class="layui-hide" id="product-table" lay-filter="product-table"></table>
                    <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="product-form">
    <div class="layui-card">
        <div class="layui-card-header">商品目录详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="product-form">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label">*通用名</label>
                <div class="layui-input-block">
                    <input type="text" name="commonName" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品名</label>
                <div class="layui-input-block">
                    <input type="text" name="productName" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">规格</label>
                <div class="layui-input-block">
                    <input type="text" name="spec" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">厂家</label>
                <div class="layui-input-block">
                    <input type="text" name="factory" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单位</label>
                <div class="layui-input-block">
                    <input type="text" name="unit" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select id="state" name="state">
                        <option value="">请选择状态</option>
                        <option value="1">启用</option>
                        <option value="2">禁用</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="form-submit">确定</button>
            </div>
            </form>
        </div>
    </div>
</script>
<script src="layui/layui.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form' , 'laydate'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var popupIndex;

        table.render({
            elem: '#product-table',
            url: 'products',
            title: '',
            skin: 'line',
            cols: [[
                {field: 'commonName', title: '通用名'},
                {field: 'productName', title: '商品名'},
                {field: 'spec', title: '规格'},
                {field: 'factory', title: '厂家'},
                {field: 'unit', title: '单位'},
                {field: 'stateText', title: '状态'},
                {fixed: 'right', title: '操作', toolbar: '#toolbar-operation'}
            ]],
            page: true
        });

        function showProductForm() {
            popupIndex = admin.popupRight({
                area: '360px',
                success: function () {
                    var formHtml = $('#product-form').html();
                    $('#' + this.id).html(formHtml);
                    form.render();
                }
            });
            return popupIndex;
        }

        function closePopupForm() {
            if (popupIndex) {
                layer.close(popupIndex);
            }
        }

        function search(){
            table.reload('product-table', {
                where: {
                    commonName: $('#search-common-name').val(),
                    productName: $('#search-product-name').val(),
                    spec: $('#search-spec').val(),
                    state: $('#search-state').val()
                }
            });
        }

        $("#btn-search").on('click', function () {
            search();
        })

        $('#keyword').keypress(function(e){
            if(e.which==13){
                search();
            }
        });

        $("#btn-add").on('click', function () {
            popupIndex = showProductForm();
        })

        $("#btn-refresh").on('click', function () {
            table.reload('product-table');
            layer.msg('表格已刷新');
        })

        table.on('tool(product-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除该记录吗？', function (index) {
                    $.ajax({
                        url: 'products/' + data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 0) {
                                layer.msg('删除成功');
                                table.reload('product-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                showProductForm();
                form.val('product-form', data);
            }
        });

        form.on('submit(form-submit)', function (data) {
            $.ajax({
                url: 'products',
                type: 'PUT',
                data: data.field,
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg('提交成功');
                        table.reload('product-table');
                    } else {
                        layer.msg('提交失败');
                        console.error('提交失败');
                    }
                }
            });
            closePopupForm();
            return false;
        });

    });
</script>
</body>
</html>